import axios from 'axios'
import { MessageBox } from 'element-ui'

// create an axios instance
const service = axios.create({
  baseURL: window.BASEURL || '',
  timeout: 50000 // request timeout
})

service.cancelToken = axios.CancelToken

// request interceptor
service.interceptors.request.use(
  config => {
    config.headers['accept-language'] = 'zh-Hans'

    // if (store.getters.token) {
    //   config.headers['authorization'] = 'Bearer ' + store.getters.token
    // }

    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// response interceptor
service.interceptors.response.use(
  response => {
    const res = response.data
    return res
  },
  error => {
    if (error.status === 401) {
      MessageBox.confirm(
        '您已注销，可以取消以停留在此页面，或重新登录',
        '确认注销',
        {
          confirmButtonText: '重新登陆',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {})
    }

    return Promise.reject(error)
  }
)

export default service
